package com.iven.xie.redis.controller;

import com.google.common.collect.Maps;
import com.iven.xie.redis.beans.CacheKey;
import com.iven.xie.redis.model.CacheTemplate;
import com.iven.xie.redis.beans.ResObject;
import com.iven.xie.redis.service.CacheService;
import com.iven.xie.redis.service.CacheTemplateService;
import com.iven.xie.redis.util.JacksonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author iven.xie
 */
@Api(tags = "缓存工具类对应接口")
@RestController
@Slf4j
@RequestMapping("/cache")
public class CacheController {

    @Resource
    private CacheService cacheService;
    @Resource
    private CacheTemplateService cacheTemplateService;

    @GetMapping("/pageList")
    @ApiOperation(value = "查询缓存列表", notes = "查询缓存列表")
    @ResponseBody
    public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,
                                        @RequestParam(required = false, defaultValue = "10") int length, String key) {
        return cacheTemplateService.pageList(start, length, key);
    }

    @PostMapping("/save")
    @ApiOperation(value = "增加缓存", notes = "增加缓存")
    @ResponseBody
    public ResObject save(CacheTemplate cacheKey) {
        return cacheTemplateService.save(cacheKey);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新缓存", notes = "增加缓存")
    @ResponseBody
    public ResObject update(CacheTemplate cacheKey) {
        return cacheTemplateService.update(cacheKey);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除缓存", notes = "删除缓存")
    @ResponseBody
    public ResObject delete(int id) {
        return cacheTemplateService.delete(id);
    }


    @GetMapping("/getCacheInfo")
    @ApiOperation(value = "获取缓存信息", notes = "获取缓存信息")
    @ResponseBody
    public ResObject getCacheInfo(String key, String param) {
        String[] params = param != null ? param.split(",") : null;
        CacheKey cacheKey = new CacheKey(key, params);

        // 查询缓存
        Object cacheVal = cacheService.get(cacheKey);
        if (cacheVal == null) {
            return ResObject.error("缓存数据为空");
        }

        // 封装返回
        String info = cacheVal.toString();
        String type = cacheVal.getClass().getName();
        int length = 1;
        if (cacheVal instanceof List) {
            length = ((List) cacheVal).size();
        }
        String json = JacksonUtil.writeValueAsString(cacheVal);

        Map<String, Object> cacheInfo = Maps.newHashMap();
        cacheInfo.put("finalKey", cacheKey.getFinalKey());
        cacheInfo.put("type", type);
        cacheInfo.put("length", length);
        cacheInfo.put("info", info);
        cacheInfo.put("json", json);

        return ResObject.success(cacheInfo);
    }

    @PostMapping("/removeCache")
    @ApiOperation(value = "获取缓存信息", notes = "获取缓存信息")
    @ResponseBody
    public ResObject removeCache(String key, String param) {
        String[] params = param != null ? param.split(",") : null;
        CacheKey cacheKey = new CacheKey(key, params);

        boolean ret = cacheService.delete(cacheKey);
        return ret ? ResObject.success() : ResObject.error();
    }

}
